from rest_framework.viewsets import ModelViewSet
from aaa import models
from rest_framework import serializers
from rest_framework.response import Response
import json
from django.http import JsonResponse
from aaa.utils.pager import myPageNumberPagination

class orderSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Order
        fields = ['order_id','order_number','order_price','pay_status','is_send','consignee_addr','create_time',
                  'update_time','goods']

class orderInfoView(ModelViewSet):
    queryset = models.Order.objects.all()
    serializer_class = orderSerializer
    authentication_classes = []

    def list(self, request, *args, **kwargs):
        pg = myPageNumberPagination()
        xx = pg.paginate_queryset(queryset=self.queryset,request=request,view=self)
        ser = orderSerializer(instance=xx,many=True)
        ret = {}
        ret['data'] = json.dumps(ser.data)
        ret['total'] = self.queryset.count()
        return JsonResponse(ret)

    # def retrieve(self, request, *args, **kwargs):
    #     print('retrieve res', args,kwargs)
    #     data = request.data
    #     try:
    #         obj = list(models.Order.objects.filter(order_id=kwargs['pk']).values('order_id','order_number','order_price','pay_status','is_send','consignee_addr','create_time',
    #               'update_time','goods'))
    #         goods_obj = [ list(models.Goods.objects.filter(goods_id=item['goods']).values('goods_id','goods_cat','goods_state','goods_name','goods_number','is_promote','hot_number',
    #               'goods_price','goods_weight','pics','add_time','upd_time','attrs','goods_introduce'))[0] for item in obj]
    #         print('goods_obj',goods_obj)
    #         obj[0]['goods'] = goods_obj
    #         print(print('obj',obj[0]))
    #         return JsonResponse({'data':obj[0]})
    #     except Exception as e:
    #         print('e',e)